# -*- coding: utf-8 -*-
# @Time    : 2022/2/13 15:33
# @Author  : Sansui
# @FileName: huluxia.py
# @Software: PyCharm
# @Blog    ：https://www.cnblogs.com/sansui6/

import requests
import hashlib
import json
import time
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
user = ""
passwd = ""
r = requests.Session()

# pushplus
pushplus_token = ""

def md5(passwd):
    m = hashlib.md5()
    b = passwd.encode(encoding='utf-8')
    m.update(b)
    passwd_md5 = m.hexdigest()
    return passwd_md5

# 登录
def login():
    passwd_md5 = md5(passwd)
    url = 'http://floor.huluxia.com/account/login/ANDROID/4.0?device_code=1'
    params = {
        'account': user,
        'login_type': '2',
        'password': passwd_md5
    }
    headers = {
        'Connection': 'close',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': '112',
        'Host': 'floor.huluxia.com',
        'Accept-Encoding': 'gzip',
        'User-Agent': 'okhttp/3.8.1'
    }
    user_info = requests.post(url=url, data=params, headers=headers).json()
    login_error_info = user_info["msg"]
    if ("账号或密码错误" in login_error_info):
        print("登录失败:" + login_error_info);
        return;

    # 获取用户信息
    user_id = user_info["user"]["userID"]
    user_nick = user_info["user"]["nick"]
    # 用户登录密匙
    user_key = user_info["_key"]
    session_key = user_info["session_key"]
    key = {
        "user_id": user_id,
        "user_nick": user_nick,
        "user_key": user_key,
        "session_key": session_key
    }
    print(user_info)
    return key

# 检测登录信息
def check_login(user_key):
    url = 'http://floor.huluxia.com/user/status/ANDROID/2.1';
    params = {
        'platform': '2',
        'gkey': '000000',
        'app_version': '4.0.0.6.3',
        'versioncode': '20141434',
        'market_id': 'floor_baidu',
        '_key': user_key,
        'device_code': '%5Bw%5D02%3A00%3A00%3A00%3A00%3A00',
    }
    check_info = requests.get(url=url, params=params).json()
    print(check_info)
    return check_info;

# 获取论坛板块信息
def get_category(user_key):
    category = []
    url = 'http://floor.huluxia.com/category/list/ANDROID/2.0'

    params = {
        'platform': '2',
        'gkey': '000000',
        'app_version': '4.0.0.6.3',
        'versioncode': '20141434',
        'market_id': 'floor_huluxia',
        '_key': user_key,
        'device_code': '%5Bw%5D02%3A00%3A00%3A00%3A00%3A00',
        'is_hidden': '1'
    }

    category_info = requests.get(url=url, params=params).json()
    # 排除无法签到的板块
    exclude_str = ["我的关注", "三楼活动", "系统推荐", "我的版块"]
    for x in category_info["categories"]:
        # print(str(x["categoryID"])+":"+x["title"])
        if (x["title"] not in exclude_str):
            # print(str(x["categoryID"])+":" + x["title"])
            categoryStr = {
                "categoryID": x["categoryID"],
                "title": x["title"]
            }
            category.append(categoryStr)
    return category;

# 签到
def signin(user_key):
    url = 'http://floor.huluxia.com/user/signin/ANDROID/4.0';
    key = user_key

    push_data = []

    for x in get_category(key):
        categoryId = x["categoryID"]
        title = x["title"]
        params = {
            'platform': '2',
            'gkey': '000000',
            'app_version': '4.0.0.6.3',
            'versioncode': '20141434',
            'market_id': 'floor_baidu',
            '_key': key,
            'device_code': '%5Bw%5D02%3A00%3A00%3A00%3A00%3A00',
            'cat_id': categoryId
        }

        headers = {
            'Connection': 'close',
            'Host': 'floor.huluxia.com',
            'Accept-Encoding': 'gzip',
            'User-Agent': 'okhttp/3.8.1'
        }
        signin_info = r.get(url=url, params=params,headers=headers).json()

        if(signin_info["status"].__eq__(1)):
            time.sleep(10)
            push_data.append(title +'签到成功')

    return push_data;

def push(user_key):

    title = '葫芦侠签到';
    push_data = signin(user_key)
    url = f"http://pushplus.hxtrip.com/send?token={pushplus_token}&title={title}&content={push_data}&template=html";

    res = requests.post(url);
    if res.status_code == 200:
        print("成功！")


def main():
    user_key = login()["user_key"]
    check_login(user_key)
    push(user_key)



if __name__ == "__main__":
    main()


def main_handler(event, context):
    return main()